import { enumCode } from '/@/const';
import { useConfirmDialog } from '/@/hooks';
import { isNull, showError, showSuccess, showWarning } from '/@/utils';
//接口
import { InvRuKuApi, InvRuKuMingXiInput, InvRuKuOutput } from '/@/api/inv/InvRuKu';
import { XieYiYiYuanChuKuApi, XieYiYiYuanChuKuOutput } from '/@/api/inv/invXieYiYiYuanChuKu';
import useChuKuDanInfo from './chuKuDanInfo';
import { initXueYePinZhongListForce, loginInfo, xueYePinZhongList } from '/@/stores/baseInfo';

//#region dom元素
export const billPanelRef = ref();
export const formRef = ref();
export const listRef = ref();
//#endregion

//#region 初始上下文对象
const ctxinitialState = () => ({
	isEdit: false,
	loading: false,
	// 血液来源默认本院
	entity: { danJuLeiXing: '协议医院入库', xueYeLaiYuanId: 1300000000001, ruKuYuanQuId: loginInfo.value.yuanQuId } as InvRuKuOutput,
	mingXiTable: [] as InvRuKuMingXiInput[],
});
//#endregion
//#region 上下文对象
export const ctx = reactive(ctxinitialState()); // 使用 reactive 初始化
//#endregion
//#region 单据操作方法集
export const fnBill = () => {
	/** 清空表单数据 */
	const newEntity = () => {
		//实体值ctx初始化
		Object.assign(ctx, ctxinitialState());
		ctx.isEdit = true;
	};

	/** 加载单据 */
	const loadEntity = (entity: InvRuKuOutput) => {
		if (isNull(entity) || isNull(entity.id)) {
			return;
		}
		//调用接口获取数据
		InvRuKuApi.get(entity.id).then((res) => {
			formRef.value.resetFields();
			//赋值实体
			ctx.entity = res.master;
			//赋值明细
			ctx.mingXiTable = res.mingXiList;
			ctx.isEdit = false;
		});
	};
	/** 提交 */
	const submit = async () => {
		// 表单验证
		const validate = await formRef?.value?.validate();
		if (!validate) return;
		if (ctx.mingXiTable.length === 0) {
			showError('请选择来源单号获取血液明细');
			return;
		}
		if (isNull(ctx.entity.id)) {
			ctx.entity.danJuZhuangTai = enumCode.Djzt.已生效;
			ctx.entity = await InvRuKuApi.add({ ...ctx.entity, mingXiList: ctx.mingXiTable });
		} else {
			// 入库后明细不能再修改, 只能修改主表
			// await InvRuKuApi.update({ ...ctx.entity, mingXiList: ctx.mingXiTable });
		}
		showSuccess();
		ctx.isEdit = false;
	};
	/** 作废 */
	const invalidate = () => {
		useConfirmDialog('确认作废吗?').then(() => {
			ctx.entity = { danJuZhuangTai: enumCode.Djzt.已作废 } as InvRuKuOutput;
			// TODO 调用单据作废接口
			showSuccess('已作废');
		});
	};
	return { newEntity, loadEntity, submit, invalidate };
};
//#endregion
//#region 单据明细方法集
export const fnMingXi = () => {
	/** 选择出库单 */
	const chooseChuKuDan = () => {
		useChuKuDanInfo().then((value) => {
			const row = value as XieYiYiYuanChuKuOutput;
			console.log(row);
			ctx.entity.laiYuanDanHao = row.danHao;
			getChuKuDanMingXi();
		});
	};
	/** 获取出库单明细 */
	const getChuKuDanMingXi = () => {
		if (!isNull(ctx.entity.laiYuanDanHao)) {
			ctx.loading = true;
			XieYiYiYuanChuKuApi.xieYiYiYuanChuKuMingxi({ danHao: ctx.entity.laiYuanDanHao, ruKuYuanQuId: loginInfo.value.yuanQuId })
				.then((res: any) => {
					const { master, mingXiList } = res;
					if (isNull(master)) {
						showError('出库单不存在');
						return;
					}
					// 获取明细时会更新品种表，所以要重新初始化
					initXueYePinZhongListForce().then(() => {
						ctx.mingXiTable = mingXiList;
						if (ctx.mingXiTable == null || ctx.mingXiTable.length == 0) {
							showWarning('出库单不存在未入库的血液');
							return;
						}
					});
				})
				.finally(() => {
					ctx.loading = false;
				});
		}
	};
	/** 删除血液 */
	const deleteBlood = (row: any) => {
		useConfirmDialog(`是否确认删除血液${row.xianXueMa}`).then(() => {
			ctx.mingXiTable.splice(ctx.mingXiTable.indexOf(row), 1);
			showSuccess('删除成功');
		});
	};
	return {
		/** 选择出库单 */
		chooseChuKuDan,
		/** 获取出库单明细 */
		getChuKuDanMingXi,
		/** 删除列表血液 */
		deleteBlood,
	};
};
//#endregion

//#region 打印功能方法集
export const fnPrint = () => {
	/** 打印明细 */
	const printMingXiObj = {
		id: 'printMingXi', //打印的html标签id
		popTitle: '',
		extraCss: '',
		extraHead: '<meta http-equiv="Content-Language" content="zh-cn"/>',
	};

	/** 打印全部 */
	const printAllObj = {
		id: 'printAll', //打印的html标签id
		popTitle: '',
		extraCss: '',
		extraHead: '<meta http-equiv="Content-Language" content="zh-cn"/>',
	};
	// const printALL = () => {
	// 	billPanelRef.value.printWeb('billno', 'all');
	// 	showSuccess('printALL');
	// };
	return { printMingXiObj, printAllObj };
};
//#endregion
